package com.agileai.pm.module.proc.handler;

import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.TreeBuilder;
import com.agileai.hotweb.domain.TreeModel;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProcPlanAdminManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;

public class ProcPlanAdminManageListHandler
        extends StandardListHandler {
    public ProcPlanAdminManageListHandler() {
        super();
        this.editHandlerClazz = ProcPlanAdminManageEditHandler.class;
        this.serviceId = buildServiceId(ProcPlanAdminManage.class);
    }

	public ViewRenderer prepareDisplay(DataParam param){
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}    
    
    protected void processPageAttributes(DataParam param) {
    
    }

    protected void initParameters(DataParam param) {
    	Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
    	String projectId = project.getProjectId();
        initParamItem(param, "projectId", projectId);
    }
    
    @PageAction
    public ViewRenderer loadPlanData(DataParam param){
    	String responseText = "";
    	String projectId = param.get("projectId");
    	List<DataRow> rsList = getService().findRecords(param);
    	
    	TreeBuilder treeBuilder = new TreeBuilder(rsList, "PPA_ID", "PPA_NAME", "PPA_PID");
    	treeBuilder.setRootId(projectId);
    	TreeModel treeModel = treeBuilder.buildTreeModel();

    	try {
        	JSONArray jsonArray = new JSONArray();
        	List<TreeModel> topChildren = treeModel.getChildren();
        	for (int i=0;i < topChildren.size();i++){
        		JSONObject jsonObject = new JSONObject();
        		TreeModel child = topChildren.get(i);
        		jsonObject.put("ppaId", child.getId());
        		jsonObject.put("ppaName", child.getName());
        		jsonObject.put("ppaType", child.getProperty().get("PPA_TYPE"));
        		jsonObject.put("ppaContent", child.getProperty().get("PPA_CONTENT"));
        		jsonObject.put("ppaCharge", child.getProperty().get("PPA_CHARGE"));
        		jsonObject.put("ppaWorkDay", child.getProperty().get("PPA_WORK_DAY"));
        		jsonObject.put("ppaStartTime", child.getProperty().get("PPA_START_TIME"));
        		jsonObject.put("ppaState", child.getProperty().get("PPA_STATE_NAME"));
        		jsonObject.put("ppaEndTime", child.getProperty().get("PPA_END_TIME"));
        		
        		if (child.getChildren().size() > 0){
        			JSONArray jsonArrayChild = new JSONArray();
        			this.loopBuildJson(child, jsonArrayChild);
        			jsonObject.put("children",jsonArrayChild);
        		}
        		jsonArray.put(jsonObject);
        	}	
        	responseText = jsonArray.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
    	
    	return new AjaxRenderer(responseText);
    }
    
    private void loopBuildJson(TreeModel treeModel,JSONArray jsonArray) throws JSONException{
    	List<TreeModel> topChildren = treeModel.getChildren();
    	for (int i=0;i < topChildren.size();i++){
    		JSONObject jsonObject = new JSONObject();
    		TreeModel child = topChildren.get(i);
    		jsonObject.put("ppaId", child.getId());
    		jsonObject.put("ppaName", child.getName());
    		jsonObject.put("ppaType", child.getProperty().get("PPA_TYPE"));
    		jsonObject.put("ppaContent", child.getProperty().get("PPA_CONTENT"));
    		jsonObject.put("ppaCharge", child.getProperty().get("PPA_CHARGE"));
    		jsonObject.put("ppaWorkDay", child.getProperty().get("PPA_WORK_DAY"));
    		jsonObject.put("ppaStartTime", child.getProperty().get("PPA_START_TIME"));
    		jsonObject.put("ppaState", child.getProperty().get("PPA_STATE_NAME"));
    		jsonObject.put("ppaEndTime", child.getProperty().get("PPA_END_TIME"));
    		
    		if (child.getChildren().size() > 0){
    			JSONArray jsonArrayChild = new JSONArray();
    			this.loopBuildJson(child, jsonArrayChild);
    			jsonObject.put("children",jsonArrayChild);
    		}
    		jsonArray.put(jsonObject);
    	}
    }
    
    protected ProcPlanAdminManage getService() {
        return (ProcPlanAdminManage) this.lookupService(this.getServiceId());
    }
}